package org.xqyy.eval.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.xqyy.eval.dao.IUserDao;
import org.xqyy.eval.dao.TUserMapper;
import org.xqyy.eval.model.TUser;
import org.xqyy.eval.model.User;
import org.xqyy.eval.service.IUserService;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

/**
 * Created by luoxiaoming on 16-12-26.
 */

@Component
@Transactional
public class UserService implements IUserService {

    @Autowired
    private IUserDao userDao;

    @Autowired
    private TUserMapper userMapper;

    @Override
    @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, readOnly = false)
    public void add(Map<String, Object> params) {
        userDao.add(params);
    }

    @Override
    @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, readOnly = false)
    public void modify(Map<String, Object> params) {
        userDao.modify(params);
    }

    @Override
    @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, readOnly = false)
    public void delete(Map<String, Object> params) {
        userDao.delete(params);
    }

    @Override
    public Map<String, Object> loadOne(Map<String, Object> params) {
        return userDao.loadOne(params);
    }

    @Override
    public List<Map<String, Object>> load(Map<String, Object> params) {
        return userDao.load(params);
    }

    @Override
    public List<Map<String, Object>> loadSingle(Map<String, Object> params) {
        return userDao.loadSingle(params);
    }

    /**
     * 查询所有用户
     *
     * @return
     */
    @Override
    public List<TUser> selectAllUser() {
        return userMapper.selectAll();
    }

    /**
     * 查询所有用户包括科室信息
     * @return
     */
    public List<TUser> selectAllUserAndTDept(){
        return userMapper.selectAllUserAndTDept();
    }

    /**
     * 新的添加用户
     *
     * @param user
     * @return
     */
    @Override
    public Boolean addTUser(TUser user) {
        user.setId(null);
        userMapper.insert(user);
        return true;
    }

    /**
     * 新的修改用户
     *
     * @param user
     * @return
     */
    @Override
    public Boolean updateTUser(TUser user) {
        userMapper.updateByPrimaryKey(user);
        return true;
    }

    /**
     * 改变用户状态
     *
     * @param user
     * @return
     */
    @Override
    public Boolean changTUserStatus(TUser user) {
        TUser tUser = userMapper.selectByPrimaryKey(user.getId());
        if ("1".equals(tUser.getStatus())){
            tUser.setStatus("0");
        }else{
            tUser.setStatus("1");
        }
        userMapper.updateByPrimaryKey(tUser);
        return true;
    }

    /**
     * 查询一个用户
     *
     * @param user
     * @return
     */
    @Override
    public TUser findTUserById(TUser user) {
        return userMapper.selectByPrimaryKey(user.getId());

    }

    /**
     * 用户是否可以登录
     *
     * @param user 用户实体对象
     * @return
     */
    @Override
    public Boolean userLogin(TUser user) {
        user.setUsername(user.getUsername().toUpperCase());
        Example example = new Example(TUser.class);
        example.createCriteria().andEqualTo("username",user.getUsername())
                .andEqualTo("password",user.getPassword())
                .andEqualTo("status","1");

        if (userMapper.selectCountByExample(example)> 0) {
            return true;
        }
        return false;
    }

    /**
     * 根据用户名和密码查询用户
     *
     * @param user 用户信息
     * @return
     */
    @Override
    public TUser findUserByUserNameAndPassword(TUser user) {
        Example example = new Example(TUser.class);
        example.createCriteria().andEqualTo("username",user.getUsername())
                .andEqualTo("password",user.getPassword());
        return  userMapper.selectOneByExample(example);

    }

    /**
     * 修改密码
     *
     * @param user 用户信息
     * @return
     */
    @Override
    public Boolean alertPassword(TUser user) {
        TUser newuser = userMapper.selectByPrimaryKey(user.getId());
        newuser.setPassword(user.getPassword());
        userMapper.updateByPrimaryKey(newuser);
        return true;
    }
}
